/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package de.fuberlin.kosmik.gui.view.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import de.fuberlin.kosmik.R;
import de.fuberlin.kosmik.data.osmt.OsmtBundle;
import de.fuberlin.kosmik.data.osmt.OsmtFeatureRecord;
import de.fuberlin.kosmik.gui.activity.TagBundleActivity;

/**
 * Acts as a bridge between (ListView) lstBundleView in TagBundleActivity and
 * the underlying data for that view.
 * 
 * Fill the auto-complete list with key names of all known tag-keys
 * 
 * @author Johannes
 * @see TagBundleActivity
 * 
 */
public class TagBundleListAdapter extends ArrayAdapter<OsmtFeatureRecord> {

	private static final int TAG_BUNDLE_LIST_LAYOUT = R.layout.tag_bundle_list_row;

	private final Activity activity;
	private final OsmtBundle osmtBundle;

	/**
	 * Constructor.
	 * 
	 * @param activity The current context.
	 * @param osmtBundle The bundle of tags that should be displayed
	 */
	public TagBundleListAdapter(final Activity activity, final OsmtBundle osmtBundle) {
		super(activity, TAG_BUNDLE_LIST_LAYOUT, osmtBundle.getAllFeatures());
		this.activity = activity;
		this.osmtBundle = osmtBundle;
	}

	/**
	 * Record for the views in a row.
	 */
	public static class ListItem {
		/**
		 * May the key edited?
		 */
		public boolean fixKey = false;
		/**
		 * The view the key entered.
		 */
		public TextView keyView;
		/**
		 * May the value edited?
		 */
		public boolean fixValue = false;
		/**
		 * The view the value entered.
		 */
		public TextView valueView;
		/**
		 * Store here the complete feature data.
		 */
		public OsmtFeatureRecord feature;

		/**
		 * Inflates the tag contained data into the passed list item.
		 * 
		 * @param feature The record that contains information.
		 * @param listItem lisItem to modify.
		 * @post contains information from tag
		 */
		public static void setListItem(final OsmtFeatureRecord feature, final ListItem listItem) {
			// set name of tag
			listItem.keyView.setText(feature.osmKey);
			listItem.fixKey = !feature.modifiable;
			// set name of tag
			listItem.valueView.setText(feature.osmValue);
			listItem.fixValue = !feature.modifiable;
			listItem.feature = feature;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final View currentItemView;

		ListItem listItem;
		// fill text and image of item (row)
		if (convertView == null) {
			currentItemView = activity.getLayoutInflater().inflate(TAG_BUNDLE_LIST_LAYOUT,
					null, true);
			listItem = new ListItem();
			listItem.keyView = (TextView) currentItemView
					.findViewById(R.id.tagBundleRow_keyView);
			listItem.valueView = (TextView) currentItemView
					.findViewById(R.id.tagBundleRow_valueView);
			currentItemView.setTag(listItem);
		} else {
			// if row-object already exists, use it
			currentItemView = convertView;
			listItem = (ListItem) currentItemView.getTag();
		}

		OsmtFeatureRecord feature = getItem(position);

		ListItem.setListItem(feature, listItem);

		// for better contrast toggle background of rows
		currentItemView.setBackgroundColor(position % 2 != 0 ? R.color.tracklist_element_odd
				: R.color.tracklist_element_even);

		return currentItemView;
	}
}